/** bcmi_frame task interface
 *
 *  bcmi_frame task interface
 *
 *  @author : huicong
 *  @date : 2008-10-30
 *
 */
#ifndef _FRAME_TASK_H
#define _FRAME_TASK_H

#include "BasicHeader.h"
#include "MPIReader.h"
#include "MPIWriter.h"

#include <map>
#include <set>

namespace bcmi_frame
{

    enum TaskType
    {
        INITIALIZE,
        FINALIZE,
        WORKER
    };

    /** //UNDO
     *
     *
     */
    VIRTUAL_CLASS class FrameTask
    {
    public:
        typedef std::map<UINT, MPIReader*> READER_MAP;
        typedef std::map<UINT, MPIWriter*> WRITER_MAP;
        typedef std::set<UINT> RECV_SET;

    public:
        FrameTask();

        virtual ~FrameTask();

    public:
        /** return next task name, will be translate to id */
        virtual STRING nextTaskName() const = 0;

        /** task MAIN FUNCTION */
        virtual HRESULT execute() = 0;

        /** Task main function which be called by master node similar as execute().
         *
         *  This function is called by master node before the execute() be called by rest node.
         *  In fact, this function recv buffer which be send by last task from rest node
         *  and then generate user info of current task and send to rest node, 
         *  and after that, the rest node will run execute().
         *
         *  NOTE : The initialize task will not call this function, but the finalize will do
         *
         */
        virtual HRESULT masterHandler() = 0;

        /** user define initialize, before execute */
        virtual HRESULT initialize() = 0;

        /** user define finalize, after execute */
        virtual HRESULT finalize() = 0;

    public:

        HRESULT frameInitialize();

        HRESULT frameInitializeRecv();

        HRESULT frameFinalize();

        HRESULT frameFinalizeSend();

        VOID insertRecvId(IN UINT _id);

        VOID linkJob(_IN FrameJob *_job);

        VOID setTaskName(IN STRING _name);

        VOID setTaskId(IN UINT _id);

        VOID setTaskType(IN TaskType _type);

        UINT getTaskId() const;

        STRING getTaskName() const;

        TaskType getTaskType() const;

        StreamReader* getReader(IN UINT _pid);

        StreamWriter* getWriter(IN UINT _pid);

        BOOL beRegistered();

    protected:
        FrameJob *_myJob;

        UINT _taskId;
        STRING _taskName;
        BOOL _beRegistered;
        TaskType _taskType;

        /** The set which this process recv buffer from */
        RECV_SET _recvSet;

    public:
        WRITER_MAP _writer;
        READER_MAP _reader;
    };

}; //namespace bcmi_frame

#endif //#ifndef _FRAME_TASK_H